<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="150px">
      <el-form-item label="主键ID" prop="sysid">
        <el-input
          v-model="queryParams.sysid"
          placeholder="请输入主键ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="丛书题名" prop="seriestitle">
        <el-input
          v-model="queryParams.seriestitle"
          placeholder="请输入丛书题名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="正文语种" prop="textlang">
        <el-input
          v-model="queryParams.textlang"
          placeholder="请输入正文语种"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ISBN" prop="isbn">
        <el-input
          v-model="queryParams.isbn"
          placeholder="请输入ISBN"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="军事信息分类号" prop="miresnum">
        <el-input
          v-model="queryParams.miresnum"
          placeholder="请输入军事信息分类号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中图分类号" prop="clnum">
        <el-input
          v-model="queryParams.clnum"
          placeholder="请输入中图分类号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书名" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入书名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开本" prop="folio">
        <el-input
          v-model="queryParams.folio"
          placeholder="请输入开本"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版日期" prop="publishdate">
        <el-date-picker clearable size="small"
          v-model="queryParams.publishdate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择出版日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="metadataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <af-table-column label="主键ID" align="center" prop="sysid" />
      <af-table-column label="丛书题名" align="center" prop="seriestitle" />
      <af-table-column label="正文语种" align="center" prop="textlang" />
      <af-table-column label="ISBN" align="center" prop="isbn" />
      <af-table-column label="页码" align="center" prop="pagenum" />
      <af-table-column label="版本信息" align="center" prop="versioninfor" />
      <af-table-column label="出版地" align="center" prop="publicationplace" />
      <af-table-column label="出版者" align="center" prop="publisher" />
      <af-table-column label="内容简介" align="center" prop="contentvalidity" />
      <af-table-column label="军事信息分类号" align="center" prop="miresnum" />
      <af-table-column label="中图分类号" align="center" prop="clnum" />
      <af-table-column label="主题词" align="center" prop="subjectwords" />
      <af-table-column label="次要责任者责任方式" align="center" prop="secresperres" />
      <af-table-column label="其他责任者" align="center" prop="othrespersons" />
      <af-table-column label="责任方式" align="center" prop="responmode" />
      <af-table-column label="主要负责人" align="center" prop="mainresperson" />
      <af-table-column label="副题名" align="center" prop="subtitle" />
      <af-table-column label="书名" align="center" prop="title" />
      <af-table-column label="开本" align="center" prop="folio" />
      <el-table-column label="出版日期" align="center" prop="publishdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工程兵信息资源图书数据元对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form :inline="true" ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="丛书题名" prop="seriestitle">
          <el-input v-model="form.seriestitle" placeholder="请输入丛书题名" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="正文语种" prop="textlang">
          <el-input v-model="form.textlang" placeholder="请输入正文语种" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="ISBN" prop="isbn">
          <el-input v-model="form.isbn" placeholder="请输入ISBN" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="页码" prop="pagenum">
          <el-input v-model="form.pagenum" placeholder="请输入页码" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="版本信息" prop="versioninfor">
          <el-input v-model="form.versioninfor" placeholder="请输入版本信息" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="出版地" prop="publicationplace">
          <el-input v-model="form.publicationplace" placeholder="请输入出版地" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="出版者" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入出版者" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="内容简介" prop="contentvalidity">
          <el-input v-model="form.contentvalidity" type="textarea" placeholder="请输入内容" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="军事信息分类号" prop="miresnum">
          <el-input v-model="form.miresnum" placeholder="请输入军事信息分类号" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="中图分类号" prop="clnum">
          <el-input v-model="form.clnum" placeholder="请输入中图分类号" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="主题词" prop="subjectwords">
          <el-input v-model="form.subjectwords" placeholder="请输入主题词" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="次要责任者责任方式" prop="secresperres">
          <el-input v-model="form.secresperres" placeholder="请输入次要责任者责任方式" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="其他责任者" prop="othrespersons">
          <el-input v-model="form.othrespersons" placeholder="请输入其他责任者" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="责任方式" prop="responmode">
          <el-input v-model="form.responmode" placeholder="请输入责任方式" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="主要负责人" prop="mainresperson">
          <el-input v-model="form.mainresperson" placeholder="请输入主要负责人" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="副题名" prop="subtitle">
          <el-input v-model="form.subtitle" placeholder="请输入副题名" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="书名" prop="title">
          <el-input v-model="form.title" placeholder="请输入书名" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="开本" prop="folio">
          <el-input v-model="form.folio" placeholder="请输入开本" style="width: 217px"/>
        </el-form-item>
        <el-form-item label="出版日期" prop="publishdate">
          <el-date-picker clearable size="small"
            v-model="form.publishdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择出版日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMetadata, getMetadata, delMetadata, addMetadata, updateMetadata, exportMetadata } from "@/api/zyts/metadata";

export default {
  name: "Metadata",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工程兵信息资源图书数据元表格数据
      metadataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sysid: null,
        seriestitle: null,
        textlang: null,
        isbn: null,
        pagenum: null,
        versioninfor: null,
        publishinginfor: null,
        publicationplace: null,
        publisher: null,
        contentvalidity: null,
        miresnum: null,
        clnum: null,
        subjectwords: null,
        secresperres: null,
        othrespersons: null,
        responmode: null,
        mainresperson: null,
        subtitle: null,
        title: null,
        folio: null,
        filecontent: null,
        publishdate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sysid: [
          { required: true, message: "主键ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工程兵信息资源图书数据元列表 */
    getList() {
      this.loading = true;
      listMetadata(this.queryParams).then(response => {
        this.metadataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sysid: null,
        seriestitle: null,
        textlang: null,
        isbn: null,
        pagenum: null,
        versioninfor: null,
        publishinginfor: null,
        publicationplace: null,
        publisher: null,
        contentvalidity: null,
        miresnum: null,
        clnum: null,
        subjectwords: null,
        secresperres: null,
        othrespersons: null,
        responmode: null,
        mainresperson: null,
        subtitle: null,
        title: null,
        folio: null,
        filecontent: null,
        publishdate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sysid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资源图书数据元";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sysid = row.sysid || this.ids
      getMetadata(sysid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资源图书数据元";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sysid != null) {
            updateMetadata(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMetadata(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sysids = row.sysid || this.ids;
      this.$modal.confirm('是否确认删除资源图书数据元编号为"' + sysids + '"的数据项？').then(function() {
        return delMetadata(sysids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有资源图书数据元数据项？').then(() => {
        this.exportLoading = true;
        return exportMetadata(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
